<?php

namespace backend\modules\demo\models;

use yii\elasticsearch\ActiveRecord;

class EsLog extends ActiveRecord
{
    // Other class attributes and methods go here
    // ...


    public function attributes()
    {
        // path mapping for '_id' is setup to field 'id'
        return ['id', 'level', 'category', 'timestamp','trace','message'];
    }

//    public function rules()
//    {
//        return [
//            [['id', 'level', 'category', 'log_time', 'prefix', 'updated_at', 'message'], 'safe'],
//        ];
//    }
    /**
     * @return array This model's mapping
     */
    public static function mapping()
    {
        return [
            static::type() => [
                'properties' => [
                    'level' => ['type' => 'long'],
                    'category' => ['type' => 'string'],
                    'timestamp' => ['type' => 'long'],
                    'prefix' => ['type' => 'string'],
                    'message' => ['type' => 'string'],
                ]
            ],
        ];
    }

    /**
     * Set (update) mappings for this model
     */
    public static function updateMapping()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->setMapping(static::index(), static::type(), static::mapping());
    }

    /**
     * Create this model's index
     */
    public static function createIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->createIndex(static::index(), [
            //'settings' => [ /* ... */ ],
            'mappings' => static::mapping(),
            //'warmers' => [ /* ... */ ],
            //'aliases' => [ /* ... */ ],
            //'creation_date' => '...'
        ]);
    }

    /**
     * Delete this model's index
     */
    public static function deleteIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->deleteIndex('yii', 'log');
    }

}